import 'dart:async';

import 'package:flutter_cache_manager/flutter_cache_manager.dart';
import 'package:http/http.dart' as http;
import 'dart:io';
import 'package:path/path.dart' as p;
import 'package:path_provider/path_provider.dart';

/// 缓存管理
class EsoImageCacheManager extends CacheManager with ImageCacheManager{
  static const key = 'EsoImageCacheManagerData';

  static late EsoImageCacheManager _instance;
  factory EsoImageCacheManager() {
    _instance = EsoImageCacheManager._();
    return _instance;
  }

  EsoImageCacheManager._() : super(Config(key, fileService: EsoHttpFileService()));

  Future<String> getFilePath() async{
    var directory = await getTemporaryDirectory();
    return p.join(directory.path, key);
  }
}

class EsoHttpFileService extends FileService {
    late HttpClient _httpClient;
    EsoHttpFileService({HttpClient ?httpClient}) {
    _httpClient =  HttpClient();
    _httpClient.badCertificateCallback = (cert, host, port) => true;
  }

  @override
  Future<FileServiceResponse> get(String url, {Map<String, String>? headers}) async {
    // TODO: implement get
    final Uri resolved = Uri.base.resolve(url);
      final HttpClientRequest req = await _httpClient.getUrl(resolved);
      headers?.forEach((key, value) {
        req.headers.add(key, value);
      });
      final HttpClientResponse httpResponse = await req.close();
      final http.StreamedResponse response = http.StreamedResponse(
        httpResponse.timeout(const Duration(seconds: 60)), httpResponse.statusCode,
        contentLength: httpResponse.contentLength,
        reasonPhrase: httpResponse.reasonPhrase,
        isRedirect: httpResponse.isRedirect,
      );
      return HttpGetResponse(response);

  }

}